

package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

public class TableCliente extends javax.swing.JFrame {
    private ClienteLnLocal clienteControl;
    private List<Cliente> Lista;
    private DefaultTableModel modelo;
    private JFrame ventana2;

    public TableCliente(ClienteLnLocal clienteControl) {
        this.clienteControl= clienteControl;
        
         try{
            UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");
            
        }catch (Exception e){
            
        }
        
        initComponents();
        String nombres[]={"Id","Nombre","Telefono","Direccion","sexo"};
        
        modelo = new DefaultTableModel(nombres,0){
       public boolean isCellEditable(int row, int column){
           return false;
        }
        };
        TablaCliente.setModel(modelo);
        
        Lista=clienteControl.getTodos();
        
        for(Cliente p: Lista){
            String renglon[]= new String[5];
            renglon[0]=p.getId()+"";
            renglon[1]=p.getNombre();
            renglon[2]=p.getTelefono()+"";
            renglon[3]=p.getDireccion();
            renglon[4]=p.getSexo();
            modelo.addRow(renglon);
} 
    }
 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaCliente = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        OLA = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        TablaCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TablaClienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(TablaCliente);

        jRadioButton1.setText("Contiene");

        jRadioButton2.setText("inicia");

        OLA.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                OLAKeyPressed(evt);
            }
        });

        jButton1.setText("Registrar Cliente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(68, 68, 68)
                                .addComponent(OLA, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(32, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(97, 97, 97))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(OLA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OLAKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_OLAKeyPressed
        // TODO add your handling code here:
        List<Cliente> tmp= new ArrayList<>();
        if(jRadioButton1.isSelected()){
           tmp = clienteControl.getPorNombre(OLA.getText()+evt.getKeyChar(), TipoBusqueda.CONTIENE);
        }else{
            tmp = clienteControl.getPorNombre(OLA.getText()+evt.getKeyChar(), TipoBusqueda.INICIAR);
        }
         String nombres[]={"Id","Nombre","Telefono","Direccion","Sexo"};
        DefaultTableModel modelo= new DefaultTableModel(nombres,0){
        public boolean isCellEditable(int row, int column){
           return false;
        }
        };
        
        for(Cliente c: tmp){
           
        
            String renglon[]= new String[5];
            renglon[0]=c.getId()+"";
            renglon[1]=c.getNombre();
            renglon[2]=c.getTelefono()+"";
            renglon[3]=c.getDireccion();
            renglon[4]=c.getSexo();
            modelo.addRow(renglon);
        }
        
        TablaCliente.setModel(modelo);
        Lista=tmp;
       
    }//GEN-LAST:event_OLAKeyPressed

    private void TablaClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablaClienteMouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
           Cliente c= Lista.get( TablaCliente.getSelectedRow());
            if(ventana2==null){
                ventana2 = new Dialogo1(c, clienteControl);
            }else{
               ventana2.dispose();
              ventana2 = new Dialogo1(c, clienteControl);
            
            }
            ventana2.setVisible(true);
        }
    }//GEN-LAST:event_TablaClienteMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        new RegistroCliente(null, clienteControl, TipoVentana.VENTANA_REGISTRO).setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField OLA;
    private javax.swing.JTable TablaCliente;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
